

Public Class IOParameterService


    Private _rti As RefTypeItem

    Public Sub New(ByVal rti As RefTypeItem)
        _rti = rti
    End Sub

    Public Function GetIOParameter() As IOParameterItem
        Dim il As IList(Of IOParameterItem)
        il = New List(Of IOParameterItem)

        Dim ds As DataSet

        Dim strSql As String
        strSql = String.Format("Select * From tbccIOParameter_Offline where projectID='{0}' and Netid='{1}' and Refid='{2}'", _rti.ProjectID, _rti.NetId, _rti.RefId)

        ds = TBCC.Utility.SqlHelper.ExecuteDataset(strSql, Nothing)

        For Each r As DataRow In ds.Tables(0).Rows
            Dim ipi As New IOParameterItem()
            ipi.ProjectID = r("projectID")
            ipi.Netid = r("Netid")
            ipi.Refid = r("Refid")
            ipi.AlarmTempHigh = r("AlarmTempHigh")
            ipi.AlarmTempLow = r("AlarmTempLow")
            ipi.AlarmHumiHigh = r("AlarmHumiHigh")
            ipi.AlarmHumiLow = r("AlarmHumiLow")
            ipi.DoorAlarmDelay = r("DoorAlarmDelay")
            ipi.DoorPreAlarmDelay = r("DoorPreAlarmDelay")
            ipi.LostAlarmDelay = r("LostAlarmDelay")
            ipi.LostPreAlarmDelay = r("LostPreAlarmDelay")
            ipi.TempHighAlarmDelay = r("TempHighAlarmDelay")
            ipi.TempHighPreDelay = r("TempHighPreDelay")
            ipi.TempLowAlarmDelay = r("TempLowAlarmDelay")
            ipi.TempLowPreDelay = r("TempLowPreDelay")
            ipi.HumiHighAlarmDelay = r("HumiHighAlarmDelay")
            ipi.HumiHighPreDelay = r("HumiHighPreDelay")
            ipi.HumiLowAlarmDelay = r("HumiLowAlarmDelay")
            ipi.HumiLowPreDelay = r("HumiLowPreDelay")

            il.Add(ipi)
        Next

        If il.Count > 0 Then
            Return il(0)
        Else
            Return Nothing
        End If
    End Function

    Public Function InsertIOParameter(ByVal ipi As IOParameterItem) As Boolean
        Dim strSql As String
        strSql = "Insert Into tbccIOParameter_Offline(projectID,Netid,Refid," & _
                                                    "AlarmTempHigh,AlarmTempLow,AlarmHumiHigh,AlarmHumiLow," & _
                                                    "DoorAlarmDelay,DoorPreAlarmDelay,LostAlarmDelay,LostPreAlarmDelay," & _
                                                    "TempHighAlarmDelay,TempHighPreDelay,TempLowAlarmDelay,TempLowPreDelay," & _
                                                    "HumiHighAlarmDelay,HumiHighPreDelay,HumiLowAlarmDelay,HumiLowPreDelay) " & _
                            "Values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)"

        Dim p(18) As SQLite.SQLiteParameter
        For i As Integer = 0 To p.Length - 1
            p(i) = New SQLiteParameter()
        Next

        p(0).Value = _rti.ProjectID
        p(1).Value = _rti.NetId
        p(2).Value = _rti.RefId
        p(3).Value = ipi.AlarmTempHigh
        p(4).Value = ipi.AlarmTempLow
        p(5).Value = ipi.AlarmHumiHigh
        p(6).Value = ipi.AlarmHumiLow
        p(7).Value = ipi.DoorAlarmDelay
        p(8).Value = ipi.DoorPreAlarmDelay
        p(9).Value = ipi.LostAlarmDelay
        p(10).Value = ipi.LostPreAlarmDelay
        p(11).Value = ipi.TempHighAlarmDelay
        p(12).Value = ipi.TempHighPreDelay
        p(13).Value = ipi.TempLowAlarmDelay
        p(14).Value = ipi.TempLowPreDelay
        p(15).Value = ipi.HumiHighAlarmDelay
        p(16).Value = ipi.HumiHighPreDelay
        p(17).Value = ipi.HumiLowAlarmDelay
        p(18).Value = ipi.HumiLowPreDelay

        Return TBCC.Utility.SqlHelper.ExecuteNonQuery(strSql, p)
    End Function

    Public Function UpdateIOParameter(ByVal ipi As IOParameterItem) As Boolean
        Dim strSql As String
        strSql = "Update tbccIOParameter_Offline Set AlarmTempHigh=?,AlarmTempLow=?,AlarmHumiHigh=?,AlarmHumiLow=?," & _
                                                    "DoorAlarmDelay=?,DoorPreAlarmDelay=?,LostAlarmDelay=?,LostPreAlarmDelay=?," & _
                                                    "TempHighAlarmDelay=?,TempHighPreDelay=?,TempLowAlarmDelay=?,TempLowPreDelay=?," & _
                                                    "HumiHighAlarmDelay=?,HumiHighPreDelay=?,HumiLowAlarmDelay=?,HumiLowPreDelay=? " & _
                "Where projectID=? and Netid=? and Refid=?"

        Dim p(18) As SQLite.SQLiteParameter
        For i As Integer = 0 To p.Length - 1
            p(i) = New SQLiteParameter()
        Next

        p(0).Value = ipi.AlarmTempHigh
        p(1).Value = ipi.AlarmTempLow
        p(2).Value = ipi.AlarmHumiHigh
        p(3).Value = ipi.AlarmHumiLow
        p(4).Value = ipi.DoorAlarmDelay
        p(5).Value = ipi.DoorPreAlarmDelay
        p(6).Value = ipi.LostAlarmDelay
        p(7).Value = ipi.LostPreAlarmDelay
        p(8).Value = ipi.TempHighAlarmDelay
        p(9).Value = ipi.TempHighPreDelay
        p(10).Value = ipi.TempLowAlarmDelay
        p(11).Value = ipi.TempLowPreDelay
        p(12).Value = ipi.HumiHighAlarmDelay
        p(13).Value = ipi.HumiHighPreDelay
        p(14).Value = ipi.HumiLowAlarmDelay
        p(15).Value = ipi.HumiLowPreDelay
        p(16).Value = _rti.ProjectID
        p(17).Value = _rti.NetId
        p(18).Value = _rti.RefId

        Return TBCC.Utility.SqlHelper.ExecuteNonQuery(strSql, p)
    End Function

    Public Function DeleteIOParameter() As Boolean
        Dim strSql As String
        strSql = "Delete From tbccIOParameter_Offline Where projectID=? and Netid=? and Refid=?"

        Dim p(2) As SQLite.SQLiteParameter
        For i As Integer = 0 To p.Length - 1
            p(i) = New SQLiteParameter()
        Next

        p(0).Value = _rti.ProjectID
        p(1).Value = _rti.NetId
        p(2).Value = _rti.RefId

        Return TBCC.Utility.SqlHelper.ExecuteNonQuery(strSql, p)
    End Function

End Class
